<template>
  <div class="exam-list">
    <Title title='考试列表'/>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="Id">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="试卷名字">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="班级">
        <template slot-scope="scope">
          <span>{{ scope.row.className }}</span>
        </template>
      </el-table-column>
      <el-table-column label="题目数">
        <template slot-scope="scope">
          <span>{{ scope.row.subjects.length }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入试卷名搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type='primary' @click="handlePreview(scope.row.id, scope.row.name)"
            >开始考试</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { delPaper, getStuPapers } from "@/api/paper";
import { formatPaperData, formatSubjects } from "@/utils";
import { info, success, error } from "@/utils";
import PreviewPaper from "@/components/preview/PreviewPaper.vue";
import Title from '@/components/common/Title'
export default {
  data() {
    return {
      tableData: [],
      search: "",
    };
  },
  components: {
    PreviewPaper,
    Title
  },
  methods: {
    /**
     * 删除试卷
     */
    handleDelete({ id }) {
      this.$confirm("此操作将永久删除该试卷,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async (_) => {
          const res = await delPaper(id);
          if (res.code === 1) {
            success(res.msg);
            this.getData();
          } else {
            error(res.msg);
          }
        })
        .catch((e) => {
          info("取消删除");
        });
    },
    /**
     * 获取数据
     */
    async getData() {
     const res = await getStuPapers()
      res.data.forEach(d => {
        d.subjects = formatSubjects(d.subjects)
      })
      this.tableData = formatPaperData(res.data);
    },

    /**
     * 预览
     */
    async handlePreview(id) {
        this.$router.push({name: 'entry', query: {paperId: id}})
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped></style>
